package com.lizhi.rpc.handler;

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.common.yun.enums.DataUpStateEnum;
import com.lizhi.yun.db.stand.dao.SerialManageDao;
import com.lizhi.yun.db.stand.pojo.SerialManagePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lizhi
 * @Description:
 * @Date: create in 2020/7/13 9:28
 */
@Slf4j
@Service
public class RecvSerialManageHandler {

    @Resource
    private SerialManageDao serialManageDao;

    /**
     * 接收串口的同步信息
     * @param json
     * @return
     */
    public String recvSerialManage(String json){
        UpDataPOJO upSerialManagePOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upSerialManagePOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*串口信息*/
        SerialManagePOJO serialManagePOJO = SamJsonUtil.toObject(upSerialManagePOJO.getData(), SerialManagePOJO.class);
        if (StringUtils.isEmpty(serialManagePOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        serialManagePOJO.setHospitalId(rpcClientPOJO.getOrganId());
        serialManagePOJO.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*数据上传的时间*/
        serialManagePOJO.setDataUpState(DataUpStateEnum.DATA_UP_STATE_01.getEnValue());
        serialManagePOJO.setDataUpTime(DateUtils.getNowDate());
        log.info("上传的数据信息是serialManagePOJO:{}", serialManagePOJO);

        SerialManagePOJO serialManage = serialManageDao.selectById(serialManagePOJO.getCode(), serialManagePOJO.getHospitalId(), serialManagePOJO.getHospitalSystemId());
        if (StringUtils.isEmpty(serialManage)){
            serialManagePOJO.lid = StringUtils.newId();
            serialManageDao.insertRecord(serialManagePOJO);
        } else {
            serialManageDao.updateByIdSelective(serialManagePOJO);
        }

        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
